package com.smart.lab.fragment.dashboard;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.smart.lab.databinding.FragmentDashboardBinding;
import com.smart.lab.model.CropPestDiseaseEntity;
import com.smart.lab.model.ResponseBean;
import com.smart.lab.utils.MyUrls;
import com.smart.lab.utils.PageBean;
import com.smart.lab.utils.net.ItheimaHttp;
import com.smart.lab.utils.net.Request;
import com.smart.lab.utils.net.listener.HttpResponseListener;
import com.smart.lab.view.activity.ClipActivity;
import com.smart.lab.view.activity.ScanResultActivity;
import com.smart.lab.view.adapter.ItemListAdapterOfPest;
import com.smart.lab.view.common.Constant;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;

public class DashboardFragment extends Fragment implements OnRefreshListener, OnLoadMoreListener {


    SmartRefreshLayout refreshLayout;

    RecyclerView recyclerView;

    private int pageNum = 1;
    private int pageSize = 10;
    private int maxPage = 1;

    private ItemListAdapterOfPest mItemAdatper;

    private List<CropPestDiseaseEntity> itemList=new ArrayList<>();

    private FragmentDashboardBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);

        recyclerView=binding.recyclerView;
        refreshLayout=binding.refreshLayout;

        initView();

        return binding.getRoot();
    }


    private void initView(){
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));
        refreshLayout.autoRefresh();//界面创建时自动刷新界面
        mItemAdatper = new ItemListAdapterOfPest(itemList, getActivity());
        mItemAdatper.setOnItemClickListener(new ItemListAdapterOfPest.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, CropPestDiseaseEntity baseProjectEntity) {
//                Intent intent = new Intent(BeautifulValliageActivity.this, BeautifulValliageDetailActivity.class);
//                intent.putExtra("entity", baseProjectEntity);
//                startActivity(intent);
                Intent intent=new Intent(getActivity(), ScanResultActivity.class);
                intent.putExtra("entity",baseProjectEntity);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(mItemAdatper);
//        recyclerView.addItemDecoration(new DividerItem(this, DividerItem.VERTICAL_LIST));
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    private void startNewRequest() {
        mItemAdatper.refreshData(new ArrayList<CropPestDiseaseEntity>());

        refreshLayout.autoRefresh();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refresh(true);
    }

    private void refresh(final boolean isLoadMore) {

        if (!isLoadMore) {
            pageNum = 1;
            itemList = new ArrayList<>();
        } else {
            pageNum++;
            if (pageNum > maxPage) {
                refreshLayout.finishLoadMoreWithNoMoreData();
                return;
            }
        }

        Request request = ItheimaHttp.newGetRequest(MyUrls.listPest);
        request.putParams(Constant.PAGE, pageNum);
        request.putParams(Constant.LIMIT, pageSize);

        ItheimaHttp.send(request, new HttpResponseListener<ResponseBean<PageBean<CropPestDiseaseEntity>>>() {

            @Override
            public void onResponse(ResponseBean<PageBean<CropPestDiseaseEntity>> res) {

                if (res.getStatus() == 0) {
                    if (res.getData() != null && res.getData().getList().size() > 0) {
                        maxPage = res.getData().getTotalCount() / pageSize;
                        if (res.getData().getTotalCount() % pageSize != 0) {
                            maxPage++;
                        }
                        if (!isLoadMore) {
                            mItemAdatper.refreshData((List<CropPestDiseaseEntity>) res.getData().getList());

                        } else {
                            mItemAdatper.addData((List<CropPestDiseaseEntity>) res.getData().getList());
                        }
                    } else {
//                        showNoData();
                    }
                } else {
//                    showNoData();
//                    SendCheckLoginUtils.checkResponseCode(BeautifulValliageActivity.this, res.getStatus(), res.getMsg());
                }

                if (!isLoadMore) {
                    refreshLayout.finishRefresh();
                } else {
                    refreshLayout.finishLoadMore();
                }


            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable e) {
                super.onFailure(call, e);

                if (!isLoadMore) {
                    refreshLayout.finishRefresh();
                } else {
                    refreshLayout.finishLoadMore();
                }

//                showNoNet();

            }
        });

    }
}